/*
	FileName : JT_C_REC_Wave.c
	Description :
		Manage the format and step of JT_C wave record.
	
	Created on 2017.11.04 by WFL.

	Revision History:

*/


/* Includes ------------------------------------------------------------------*/



#include "JT_C_REC_Wave.h"
#include "JT_C_REC_Item.h"
#include "GF_WFL_PI.h"

//---------------------
JTC_REC_WAVE JTC_wave;

#define JTC_LED(led)		((uint8_t)(led & 0x1F))
//-----------------------------------------
void JTC_REC_WAVE_init(JTC_REC_WAVE *j_wave)
{
	j_wave->RecordMethod = 0;
	j_wave->newWaveStart = 0;
	
	j_wave->AnaFileSampleCnt = 0;
	j_wave->AnaFileContinousIndex = 0;
	j_wave->currentTime = 0;
	j_wave->RecordIndex = 0;
	
	j_wave->AnaFilePrevLed[0] = 0;
	j_wave->AnaFilePrevLed[1] = 0;
	j_wave->AnaFileLed[0] = 0;
	j_wave->AnaFileLed[1] = 0;
	j_wave->AnaLedNoCode[0] = 0;
	j_wave->AnaLedNoCode[1] = 0;
	j_wave->waveJYJChanged[0] = 0;
	j_wave->waveJYJChanged[1] = 0;
	
	j_wave->isKeyWave[0] = 0;
	j_wave->isKeyWave[1] = 0;
	j_wave->AnaFileValid[0] = 0;
	j_wave->AnaFileValid[1] = 0;
	j_wave->AnaFileTime[0] = 0;
	j_wave->AnaFileTime[1] = 0;
	j_wave->AnaSampleStartIndex[0] = 0;
	j_wave->AnaSampleStartIndex[1] = 0;
	
	return;
}
//---------------------------creat wave----------------------------------------
void JTC_REC_WAVE_startANewWave(JTC_REC_WAVE *jtc_W,
									uint32_t sampleindex,
									uint8_t lastled,
									uint8_t led)
{
	uint8_t index;
	
	index = jtc_W->RecordIndex;
	
	if (jtc_W->RecordMethod == RECORD_METHOD_CONTINOUS)
		return;
	
	//if need to check AnaFileValid
	
	if (!jtc_W->newWaveStart){
				
			jtc_W->AnaFileTime[index] = jtc_W->currentTime;
			jtc_W->AnaFileSampleCnt = ANALOG_BEGIN_10S_CNT;
			jtc_W->AnaSampleStartIndex[index] = Buf_index_add(0, \
												SAMPLE_BUF_SIZE, \
												sampleindex, \
												SAMPLE_BUF_SIZE-ANALOG_BEGIN_10S_CNT);
			jtc_W->AnaFileLed[index] = JTC_LED(led);
			jtc_W->AnaFilePrevLed[index] = JTC_LED(lastled);
			jtc_W->AnaLedNoCode[index] = 0;
			jtc_W->waveJYJChanged[index] = 0;
			jtc_W->newWaveStart = 1;
		}
}
///
void JTC_REC_WAVE_updateCurrWaveNocode(JTC_REC_WAVE *jtc_W,uint8_t nocode)
{
	if (jtc_W->newWaveStart){
		
		if (nocode)
			jtc_W->AnaLedNoCode[jtc_W->RecordIndex] = 1;
	}
}
///
void JTC_REC_WAVE_updateCurrWaveJYJChange(JTC_REC_WAVE *jtc_W,uint8_t jyjChange)
{
	if (jtc_W->newWaveStart){
		
		if (jyjChange)
			jtc_W->waveJYJChanged[jtc_W->RecordIndex] = 1;
	}
}
///
void JTC_REC_WAVE_updateCurrWaveLed(JTC_REC_WAVE *jtc_W,uint8_t led)
{
	uint8_t index;
	
	index = jtc_W->RecordIndex;
	
	if (jtc_W->newWaveStart){
		
		if (jtc_W->AnaFilePrevLed[index] == jtc_W->AnaFileLed[index]){
			
			jtc_W->AnaFileLed[index] = JTC_LED(led);
		}
	}
}
void JTC_REC_WAVE_newAdcDataHandler(JTC_REC_WAVE *jtc_W,uint32_t sm_index,uint8_t isPowOff)
{
	static uint32_t powoffcnt = 0;
	
	uint8_t index = jtc_W->RecordIndex;
	
	if(isPowOff)
	{
		if((powoffcnt + 1) != 0)
			powoffcnt++;
	}
	else
		powoffcnt = 0;
	
	if(powoffcnt > 1250)
	{
		return;
	}
	
	if (jtc_W->newWaveStart){
		
		jtc_W->AnaFileSampleCnt++;
		
		if (jtc_W->AnaFileSampleCnt >= ANALOG_FILE_LEN - 16){
			
			jtc_W->AnaFileValid[index] = 1;
			
			
			if (jtc_W->RecordMethod == RECORD_METHOD_CONTINOUS)
				jtc_W->isKeyWave[index] = 0;
			else
				jtc_W->isKeyWave[index] = JTC_REC_WAVE_IsKey(jtc_W,index);
			
			jtc_W->RecordIndex = Buf_index_add(0,2,jtc_W->RecordIndex,1);
			jtc_W->newWaveStart = 0;
		}
	}
	else
	{
		if (jtc_W->RecordMethod == RECORD_METHOD_CONTINOUS)
		{
			jtc_W->AnaFileSampleCnt = 0;
			jtc_W->AnaFileTime[jtc_W->RecordIndex] = jtc_W->currentTime;
			jtc_W->AnaSampleStartIndex[jtc_W->RecordIndex] = sm_index;
			jtc_W->newWaveStart = 1;
		}
	}
}

/*
  1 L-->HU;L-->B, U-->B 
  2 JYJ unchanged:
		L --> U, L -->UU,	L-->LU,
		LU-->HU, LU--> U,	LU-->UU,
		U -->HU,
		UU-->HU;
  3 nocode LED_B
  4 nocode LED_H
*/
uint8_t JTC_REC_WAVE_IsKey(JTC_REC_WAVE *jtc_W,uint8_t index)
{
	uint8_t l1, l2, changed, res = 0;
	uint16_t tmp;
	
	if (jtc_W->AnaFilePrevLed[index] == jtc_W->AnaFileLed[index])
		return 0;
	
	///previous and current LED
	l1 = jtc_W->AnaFilePrevLed[index];
	l2 = jtc_W->AnaFileLed[index];
	changed = jtc_W->waveJYJChanged[index];
	tmp = l1 | ((uint16_t)l2<<8);
	
	switch (tmp){
		
		case L_HU:
		case L_B:
		case U_B:
			res = 1;
			break;
				
		default:
			
			if (!changed){
				
				switch (tmp){
					
					case L_LU:
					case L_U:
					case L_UU:
					case LU_U:
					case LU_HU:
					case LU_UU:
					case U_HU:
					case UU_HU:
						res = 1;
						break;
				}
			}
			break;
	}
	
	if (((l2 == LED_H) || (l2 == LED_B)) && jtc_W->AnaLedNoCode[index])
			res = 1;
	
	return res;
}

//-------------------------save wave------------------------------------------
uint32_t JTC_REC_WAVE_creatHeadSector(JTC_REC_WAVE *jtc_W,uint8_t *buf,uint8_t index)
{
	uint32_t start;
	
	Mem_8b_setAll(buf,0,16,0);
	
	Mem_LE_Set32(buf,2,ANALOG_FILE_ID);
	Mem_LE_Set32(buf,6,jtc_W->AnaFileTime[index]);
	
	if (jtc_W->RecordMethod == RECORD_METHOD_CONTINOUS){
		
		Mem_LE_Set16(buf,10,jtc_W->AnaFileContinousIndex);
		jtc_W->AnaFileContinousIndex++;
	}
	buf[12] = jtc_W->RecordMethod;
	buf[14] = 125;// = TIM3_FREQ / 100;
	
	start = jtc_W->AnaSampleStartIndex[index];
	
	Buf_8b_copy(SampleBuf,SAMPLE_BUF_SIZE,start,buf + 16,512-16);
	
	start = Buf_index_add(0,SAMPLE_BUF_SIZE,start,512-16);
	
	return start;
}

void JTC_REC_WAVE_finishSavingWave(JTC_REC_WAVE *jtc_W,uint8_t index)
{
	jtc_W->AnaFileValid[index] = 0;
}
//-----------------------------------------------------------------------
///EOF
